200 F = 0:V = 30:H = 32:Y = 3: REM ** VERTICAL SORT
202 IF SP = 1 THEN SPEED= 150
205 COUNT = COUNT +1
206 PRINT
208 VTAB 23: PRINT "THIS IS PASS NUMBER "COUNT"."
220 IF HO = 1 THEN 400
300 FOR I = S TO H STEP 2
310 FOR J = S TO V STEP 2
320 IF SCRN( I,J +1) = < SCRN( I,J +Z) THEN 370
325 G = SCRN( I,J +Z)
330 COLOR= SCRN( I,J)
335 F = F +W
340 HLIN I,I +W AT J +Z: HLIN I,I +W AT J +Y
350 COLOR= G
360 HLIN I,I +W AT J: HLIN I,I +W AT J +W
370 NEXT J
372 NEXT I
380 IF VERTICAL = 1 THEN 500
400 FOR I = S TO H STEP 2: REM ** HORIZONTAL SORT
405 FOR J = S TO V STEP 2
410 IF SCRN( J,I) = > SCRN( J +Z,I) THEN 470
425 G = SCRN( J +Z,I)
430 COLOR= SCRN( J,I)
435 F = F +W
440 HLIN J +Z,J +Y AT I: HLIN J +Z,J +Y AT I +W
450 COLOR= G
460 HLIN J,J +W AT I: HLIN J,J +W AT I +W
470 NEXT J
480 NEXT I
500 IF F = 0 THEN SPEED= 255: INPUT "PRESS RETURN ";R$: GOTO 3500
515 S$ = "S.": IF F = 1 THEN S$ = "."
520 PRINT "WE JUST BUBBLED "F" TIME" +S$
525 PRINT CHR$(7)
530 GOTO 200
540 INPUT "PRESS RETURN ";R$: GOTO 2000
2000 TEXT : HOME
2010 GOSUB 2200
2020 A$ = "BUBBLE SORT"
2030 B$ = "DEMONSTRATION PROGRAM"
2040 C$ = "BY"
2050 H$ = "ALEX MAY"
2060 E$ = "243 SCHOOL HOUSE LANE"
2070 F$ = "TUCKAHOE, NJ 08250"
2080 Y = 5
2090 X$ = A$: GOSUB 2190
2100 X$ = B$: GOSUB 2190
2110 X$ = C$: GOSUB 2190
2120 X$ = H$: GOSUB 2190
2130 X$ = E$: GOSUB 2190
2140 X$ = F$: GOSUB 2190
2150 VTAB 21
2160 INPUT "PRESS RETURN ";RR$
2170 TEXT : HOME
2175 GOTO 70
2180 END
2190 X = 15 - INT(( LEN(X$)/2)):Y = Y +2: VTAB Y: HTAB X: PRINT X$: RETURN
2200 REM TITLE FRAME
2210 HOME : VTAB 2: HTAB 2: FOR X = 2 TO 38: PRINT "*";: NEXT
2220 FOR X = 3 TO 22: VTAB X: HTAB 2: PRINT "*";: HTAB 38: PRINT "*": NEXT
2230 VTAB 23: HTAB 2: FOR X = 2 TO 38: PRINT "*";: NEXT
2240 POKE 33,32: POKE 32,5: POKE 34,3: POKE 35,20
2250 VTAB 4: HTAB 1: RETURN
2260 END
2270 FOR X = 1 TO 20: PRINT "*";: NEXT : PRINT : PRINT CHR$(4)"SAVE BUBBLE SORT,D1"
2280 PRINT : PRINT "DO YOU WANT A SECOND COPY?": HTAB 12: INPUT "(Y OR N)";RR$: IF RR$ = "N" THEN END
2290 PRINT : PRINT "WHICH DRIVE?": PRINT : HTAB 12: INPUT "(1 OR 2)";X: PRINT CHR$(4)"SAVE BUBBLE SORT,D"X: END
3000 TEXT : HOME : VTAB 2: HTAB 14: INVERSE : PRINT "BUBBLE SORT": NORMAL : IF FLAG = 1 THEN RETURN
3010 PRINT : HTAB 5: PRINT "SORTING DATA INVOLVES MAKING COM-": PRINT : PRINT "PARISONS AND EXCHANGES AMONG ITEMS TO": PRINT : PRINT "BE SORTED. FOR EXAMPLE, ONE METHOD OF"
3020 PRINT : PRINT "ARRANGING A PILE OF CARDS IN NUMERIC": PRINT : PRINT "ORDER WOULD REQUIRE ONE TO LOOK AT": PRINT : PRINT "EACH CARD. IF A CARD'S NUMBER WERE": PRINT : PRINT "LESS THAN THE NUMBER OF THE CARD ABOVE"
3022 PRINT : PRINT "IT IN THE PILE, THE LOWER NUMBERED": PRINT : PRINT "CARD WOULD MOVE UP. THIS METHOD IS": PRINT : PRINT "CALLED A ";: INVERSE : PRINT "BUBBLE SORT";: NORMAL : PRINT "."
3030 PRINT : INPUT "PRESS RETURN ";R$: IF FLAG = 1 THEN RETURN
3040 FLAG = 1: GOSUB 3000:FLAG = 0: PRINT : HTAB 5: PRINT "THE APPLE CAN DEMONSTRATE BUBBLE": PRINT : PRINT "SORTS GRAPHICALLY DUE TO THE SYSTEM": PRINT : PRINT "OF ASSIGNING NUMBERS TO COLORS TO"
3050 PRINT : PRINT "ILLUMINATE SECTORS (PIXELS) ON THE": PRINT : PRINT "SCREEN. THIS DEMONSTRATION OF THE "
3060 PRINT : PRINT "OF THE BUBBLING PROCESS CAN SORT": PRINT : PRINT "COLORS VERTICALLY, HORIZONTALLY, OR": PRINT : PRINT "IN BOTH DIRECTIONS."
3070 PRINT : PRINT "TO ALLOW YOU TO SLOW DOWN THE VISUAL": PRINT : PRINT "PLOTTING SO THAT THE EXCHANGES ARE EASY": PRINT : PRINT "TO SEE."
3075 PRINT : HTAB 5: PRINT "THE IDEA FOR THIS DEMO CAME FROM": PRINT : PRINT "THE MARCH ISSUE OF CALL- A.P.P.L.E. YOU": PRINT : PRINT "MIGHT LOOK THERE FOR MORE INFORMATION."
3410 HOME : VTAB 3: INVERSE : HTAB 14: PRINT "NUMBER SORT": NORMAL : IF FLAG = 1 THEN RETURN
3412 PRINT : HTAB 5: PRINT "COMPUTERS CAN SORT LISTS OF NUM-": PRINT : PRINT "ERIC DATA INTO ORDER, TOO. THE BUBBLE": PRINT : PRINT "SORT IS AN EASY ROUTINE TO WRITE INTO": PRINT : PRINT "YOUR OWN PROGRAMS. HOWEVER, IT IS NOT"
3414 PRINT : PRINT "ESPECIALLY FAST. FOR MODERATELY LARGE": PRINT : PRINT "AMOUNTS OF DATA, 500 TO 1000 ITEMS, IT": PRINT : PRINT "MAY TAKE 10 TO 20 MINUTES.": PRINT : HTAB 5: PRINT "THE REASON THE BUBBLE TAKES TIME IS"
3416 PRINT : PRINT "THAT IT EXAMINES ONLY TWO ITEMS AT A": PRINT : PRINT "TIME AND EXCHANGES THE VALUES OF THOSE": PRINT : PRINT "TWO WHICH ARE NEXT TO EACH OTHER.":FLAG = 1: GOSUB 3030: GOSUB 3410:FLAG = 0
3418 PRINT : PRINT "THIS METHOD FORCES THE BUBBLE SORT TO": PRINT : PRINT "CONTINUE TO GO BACK AND COMPARE ITEMS": PRINT : PRINT "IT HAS ALREADY SORTED. THE REPEATED": PRINT : PRINT "COMPARISONS ARE NEEDED BECAUSE THE SORT": PRINT : PRINT "MAY FIND AN ITEM AT THE END OF THE LIST"
3420 PRINT : PRINT "WHICH BELONGS AT THE BEGINNING. WHEN": PRINT : PRINT "AN ITEM IS MOVED FROM THE END TO THE": PRINT : PRINT "BEGINNING, THE MOVE HAPPENS ONE EXCHANGE"
3422 PRINT "AT A TIME, FORCING THE COMPUTER INTO": PRINT : PRINT "MANY UNNECESSARY COMPARISONS.":FLAG = 1: GOSUB 3030: GOSUB 3410:FLAG = 0
3424 PRINT : HTAB 5: PRINT "IN THE SHORT DEMONSTRATION YOU ": PRINT : PRINT "ARE ABOUT TO SEE, THE COMPUTER DOES": PRINT : PRINT "SEVERAL JOBS. FIRST, IT USES ";: INVERSE : PRINT "DIM";: NORMAL : PRINT " TO": PRINT : PRINT "SET UP AN ARRAY"
3426 PRINT : HTAB 5: PRINT "THEN IT USES ";: INVERSE : PRINT "RND(1)";: NORMAL : PRINT " TO GENERATE"
3428 PRINT : PRINT "SOME RANDOM NUMBERS AND FILL THE ARRAY.": PRINT : PRINT "ONCE THE ARRAY IS FULL, A LIST OF THE": PRINT : PRINT "UNSORTED NUMBERS IS PRINTED ON THE ": PRINT : PRINT "LEFT SIDE OF THE SCREEN. "
3432 PRINT : HTAB 5: PRINT "THE NUMBERS ARE SORTED AND A": PRINT : PRINT "LIST OF THE SORTED NUMBERS IS PRINTED": PRINT : PRINT "ON THE RIGHT SIDE OF THE SCREEN.": PRINT : PRINT : PRINT :FLAG = 1: GOSUB 3030:FLAG = 0
3440 GOTO 4000
3449 INPUT R$: END
3450 END
3455 INPUT "PRESS RETURN ";R$: LIST 400,480
3460 INPUT "PRESS RETURN ";R$: GOTO 70
3500 TEXT : HOME : VTAB 3: HTAB 5: PRINT "THE DEMO STARTS BY CREATING A GRID": PRINT : PRINT "CONSTRUCTED OF RANDOM COLORS. IT THEN": PRINT : PRINT "GROUPS THE COLORS INTO CONNECTED BLOCKS": PRINT : PRINT "BY USING THE SCRN(I,J) FUNCTION TO"
3505 FLAG = 0:HO = 0:VERTICAL = 0:COUNT = 0
3510 PRINT : PRINT "LOOK AT THE COLOR VALUE OF A SECTOR.": PRINT : HTAB 5: PRINT "BECAUSE BLACK = 0, THE BLACK BLOCKS": PRINT : PRINT "ALWAYS MOVE UP AND TO THE RIGHT AS": PRINT : PRINT "THEY BUBBLE. THE MESSAGE 'WE JUST BUB-"
3515 PRINT : PRINT "BLED X TIMES.' REFERS TO THE NUMBER OF": PRINT : PRINT "EXCHANGES MADE ON EACH PASS."
3520 PRINT : INPUT "PRESS RETURN ";R$: GOTO 70
4000 REM ** BUBBLE SORT DEMO
4004 CLEAR
4005 DIM N(15)
4010 FOR I = 1 TO 14:N(I) = INT( RND(2) *110): NEXT
4015 HOME : VTAB 3: HTAB 3: PRINT "UNSORTED LIST": PRINT : FOR I = 1 TO 9: HTAB 4: PRINT "ITEM "I" - "N(I): NEXT
4020 FOR I = 10 TO 14: HTAB 4: PRINT "ITEM "I" - "N(I): NEXT
4025 POKE 33,17: POKE 32,23
4030 BUBBLE = 0: REM ** SET EXCHANGE COUNTER TO ZERO
4035 FOR I = 1 TO 13: REM COUNTER FOR COMPARING ITEMS
4040 IF N(I) < = N(I +1) THEN 4070: REM ** COMPARE TWO ITEMS; JUMP TO NEXT ITEM IF ALREADY IN NUMERICAL ORDER
4045 BUBBLE = BUBBLE +1: REM ** ADD 1 TO THE COUNTER
4050 TEMP = N(I): REM ** TEMPORARILY SAVE VALUE OF FIRST ITEM IN THE COMPARISON
4055 N(I) = N(I +1): REM ** CHANGE VALUE OF FIRST ITEM
4060 N(I +1) = TEMP: REM ** REPLACE VALUE OF SECOND ITEM WITH VALUE OF FIRST ITEM WHICH WAS SAVED IN TEMP
4065 REM ** AT THIS POINT AN EXCHANGE HAS OCCURRED TO HELP PUT ITEMS IN THEIR DESIRED ORDER
4070 NEXT I: REM ** CONTINUE THE LOOP
4075 IF BUBBLE = 0 THEN 4085: REM ** ITEMS ARE COMPLETELY SORTED; GO TO NEXT ROUTINE
4080 GOTO 4030: REM ** ITEMS ARE NOT SORTED YET; KEEP TRYING
4085 HOME
4090 VTAB 3: PRINT " SORTED LIST": PRINT
4095 FOR I = 1 TO 9: REM ** START NEW LOOP
4100 PRINT "ITEM "I" - "N(I): NEXT : REM ** PRINT VALUES IN SORTED ORDER
4105 FOR I = 10 TO 14: PRINT "ITEM "I" - "N(I): NEXT
4110 POKE 32,0: POKE 33,40
4115 VTAB 23
4120 INPUT "WOULD YOU LIKE TO SORT AGAIN? ";R$: IF LEFT$(R$,1) = "Y" THEN 4010
4130 GOTO 3400
4999 END
5000 REM LO-RES ALPHABET
5010 REM ** A
5020 VLIN Y +0,Y +4 AT X +0
5025 PLOT X +1,Y +0: PLOT X +1,Y +2
5030 VLIN Y +0,Y +4 AT X +2
5035 RETURN
5040 REM ** B
5045 HLIN X +0,X +1 AT Y +0: PLOT X +0,Y +1: PLOT X +2,Y +1
5050 HLIN X +0,X +1 AT Y +2: PLOT X +0,Y +3: PLOT X +2,Y +3: HLIN X +0,X +1 AT Y +4
5055 RETURN
5065 REM ** C
5070 HLIN X +0,X +2 AT Y +0: VLIN Y +1,Y +4 AT X +0
5074 HLIN X +1,X +2 AT Y +4
5075 RETURN
5079 :
5080 REM ** D
5081 :
5085 VLIN Y +0,Y +4 AT X +0
5090 PLOT X +1,Y +0: PLOT X +1,Y +4
5095 VLIN Y +1,Y +3 AT X +2
5100 RETURN
5109 :
5110 REM ** E
5111 :
5115 VLIN Y +0,Y +4 AT X +0
5120 PLOT X +1,Y +2
5125 HLIN X +1,X +2 AT Y +0
5130 HLIN X +1,X +2 AT Y +4
5135 RETURN
5139 :
5140 REM ** F
5141 :
5145 VLIN Y +0,Y +4 AT X +0
5150 HLIN X +1,X +2 AT Y +0
5155 PLOT X +1,Y +2
5160 RETURN
5164 :
5165 REM ** G
5166 :
5170 VLIN Y +0,Y +4 AT X +0
5175 HLIN X +1,X +2 AT Y +0
5180 VLIN Y +2,Y +4 AT X +2
5185 PLOT X +1,Y +4
5190 RETURN
5199 :
5200 REM ** H
5201 :
5205 VLIN Y +0,Y +4 AT X +0
5210 VLIN Y +0,Y +4 AT X +2
5215 PLOT X +1,Y +2
5220 RETURN
5225 :
5226 REM ** I
5227 :
5230 VLIN Y +0,Y +4 AT X +1
5235 RETURN
5239 :
5240 REM ** J
5241 :
5245 VLIN Y +3,Y +4 AT X +0
5250 PLOT X +1,Y +4
5255 VLIN Y +0,Y +4 AT X +2
5260 RETURN
5264 :
5265 REM ** K
5266 :
5270 VLIN Y +0,Y +4 AT X +0
5275 PLOT X +1,Y;X: PLOT X +2,Y +1
5280 VLIN Y +3,Y +4 AT X +3
5285 RETURN
5289 :
5290 REM ** L
5291 :
5295 VLIN Y +0,Y +4 AT X +0
5300 HLIN X +1,X +2 AT Y +4
5310 RETURN
5314 :
5315 REM ** M
5316 :
5320 VLIN Y +0,Y +4 AT X +0
5325 VLIN Y +1,Y +2 AT X +1
5330 VLIN Y +0,Y +4 AT X +2
5335 RETURN
5339 :
5340 REM ** N
5341 :
5345 VLIN Y +0,Y +4 AT X +0
5350 VLIN Y +1,Y +2 AT X +1
5355 VLIN Y +2,Y +3 AT X +2
5360 VLIN Y +0,Y +4 AT X +3
5365 RETURN
5369 :
5370 REM ** O
5371 :
5375 VLIN Y +0,Y +4 AT X +0
5380 PLOT X +1,Y +0: PLOT X +1,Y +4
5385 VLIN Y +0,Y +4 AT X +2
5390 RETURN
5394 :
5395 REM ** P
5396 :
5400 VLIN Y +0,Y +4 AT X +0
5405 PLOT X +1,Y +0: PLOT X +1,Y +2
5410 VLIN Y +0,Y +2 AT X +2
5415 RETURN
5419 :
5420 REM ** Q
5421 :
5425 VLIN Y +0,Y +4 AT X +0
5430 PLOT X +1,Y +0: PLOT X +1,Y +4
5435 VLIN Y +0,Y +4 AT X +3
5440 PLOT X +3,Y +4
5445 RETURN
5499 :
5500 REM ** R
5501 :
5505 VLIN Y +0,Y +4 AT X +0
5510 PLOT X +1,Y +0: PLOT X +1,Y +2: PLOT X +2,Y +1
5515 VLIN Y +3,Y +4 AT X +2
5520 RETURN
5524 :
5525 REM ** S
5526 :
5530 HLIN X +0,X +2 AT Y +0
5535 PLOT X +0,Y +1: PLOT X +1,Y +2: PLOT X +2,Y +3
5540 HLIN X +0,X +2 AT Y +4
5545 RETURN
5549 :
5550 REM ** T
5551 :
5555 HLIN X +0,X +2 AT Y +0
5560 VLIN Y +1,Y +4 AT X +1
5565 RETURN
5569 :
5570 REM ** U
5571 :
5575 VLIN Y +0,Y +4 AT X +0
5580 PLOT X +1,Y +4
5585 VLIN Y +0,Y +4 AT X +2
5590 RETURN
5594 :
5595 REM ** V
5596 :
5600 VLIN Y +0,Y +3 AT X +0
5605 PLOT X +1,Y +4
5610 VLIN Y +0,Y +3 AT X +2
5615 RETURN
5619 :
5620 REM ** W
5621 :
5625 VLIN Y +0,Y +4 AT X +0
5630 PLOT X +1,Y +3
5635 VLIN Y +0,Y +4 AT X +2
5640 RETURN
5644 :
5645 REM ** X
5650 VLIN Y +0,Y +2 AT X +0: VLIN Y +3,Y +4 AT X +0
5655 PLOT X +1,Y +2
5660 VLIN Y +0,Y +2 AT X +2: VLIN Y +3,Y +4 AT X +2
5665 RETURN
5669 :
5670 REM ** Y
5671 :
5675 VLIN Y +0,Y +1 AT X +0
5680 VLIN Y +2,Y +1 AT X +1
5685 VLIN Y +0,Y +1 AT X +2
5690 RETURN
5694 :
5695 REM ** Z
5696 :
5700 HLIN X +0,X +2 AT Y +0
5705 PLOT X +2,Y +1: PLOT X +1,Y +2: PLOT X +0,Y +3